-- ADReport
-- 埋点相关
local ADReport = class("ADReport")

-- 广告埋点Adjust平台事件名 IOS
local AdjustActiveIOS = {
    active = "vciwns",
    login = "26lpty",
    levelup	= "k325fk",
    Day2 = "7gp7sz",
    Day7 = "rv788p",
    Day30 = "rrg83g",
    Day60 = "vc62he",    
    Day90 = "473bcv",
    KOL	= "8s970e",
    firstpay = "c22g9u",
    superGamer = "kqzt8f",
    littlePay = "2zzm7q",
    middlePay = "77lstw",
    largePay = "6fbze4",
    onlevel_13 = "3rocr7",
    onlevel_22 = "gembhr",
    onlevel_25 = "uitjso",
    dc_auth_request = "qsnr40",
    dc_download_finish = "w292k2",
    dc_download_request	= "x8v1qj",
    dc_game_init_finish	= "ldkjrj",
    dc_game_init_start = "26kbuf",
    dc_mainpage	= "gjn76j",
    dc_notice_pop = "4dti6e",
    dc_pack2_finish	= "qg71lc",
    dc_pack2_request = "eeapqy",
    dc_role_create_done	= "27ha71",
    dc_role_create_start = "3w7roc",
    dc_sdk_init	= "jr7kb2",
    dc_sdk_pop = "3la4ew",
    dc_start_move = "it25u3",
    dc_update_finish = "fl6fzb",
    dc_update_start	= "4rvu8v",
    dc_activation_log = "el5uny",
}

-- 广告埋点Adjust平台事件名 Android
local AdjustActiveAndroid = {
    active = "mde4k4",
    login = "u3lzp5",
    levelup	= "tc8eqz",
    Day2 = "vo1z6d",
    Day7 = "mhakim",
    Day30 = "58ztmi",
    Day60 = "dz11fh",    
    Day90 = "e063gj",
    KOL	= "xmzj8k",
    firstpay = "bxe1ev",
    superGamer = "7zm2ed",
    littlePay = "1wg7w7",
    middlePay = "p6615d",
    largePay = "29yb04",
    onlevel_13 = "74wbmq",
    onlevel_22 = "sioaf2",
    onlevel_25 = "5os2gg",
    dc_auth_request = "1sepbp",
    dc_download_finish = "4kyad5",
    dc_download_request	= "qwmnl7",
    dc_game_init_finish	= "p57ys1",
    dc_game_init_start = "tay1vd",
    dc_mainpage	= "5l0lg8",
    dc_notice_pop = "d9kpn6",
    dc_pack2_finish	= "gqbgli",
    dc_pack2_request = "12uqck",
    dc_role_create_done	= "rklzmj",
    dc_role_create_start = "r2ylah",
    dc_sdk_init	= "poc47i",
    dc_sdk_pop = "fiis41",
    dc_start_move = "ww15yy",
    dc_update_finish = "a6ddbo",
    dc_update_start	= "54cige",
    dc_activation_log = "gsnapx",
}

function ADReport:ctor()
    if app:getPublisher() ~= Enums.Publisher.ema and not ENABLE_DEBUG_ADREPORD then
        return
    end

    self._isCreateRole = false
    -- 创角埋点
    EM:on("OnEnterRoleCreatedView", function()
        self:addActiveBuriedPointNotLogin("dc_role_create_start", 0, {server = app.session:getZoneId() or 0, account = app.channelAPI:getAccount() or ""})
    end)

    EM:on("OnRoleCreated", function(event, uid, zoneData)
        self._isCreateRole = true
        local groupId = 0
        if zoneData then
            if zoneData.utc == "UTC+08:00" then
                groupId = 1
            elseif zoneData.utc == "UTC-05:00" then
                groupId = 2
            elseif zoneData.utc == "UTC+01:00" then
                groupId = 3
            end
        end
        if groupId ~= 0 then
            self:addActiveBuriedPointAlreadyLogin("dc_role_create_done", 0, {role_id = tostring(uid or ""), group_id = groupId})
        else
            self:addActiveBuriedPointAlreadyLogin("dc_role_create_done", 0, {role_id = tostring(uid or "")})
        end
        
    end)

    EM:on("OnGameConnected", function()
        self:onGameConnectedAdReport()
    end)
    -- 登录埋点(loading完之后)
    EM:on("onMapChanged", function(eventName, isFirstEnterMap)
        if isFirstEnterMap then
            self:addAdReport({event = "login"})
            self:onPlayerRechargeChange() -- 累计充值埋点(每次登录的时候发一次，目前没有做去重)
            self:onLoginDaysAdReport() -- 累计登录埋点(每次登录的时候发一次，目前没有做去重)
            self:onIsKOLAdReport() -- 活跃用户埋点(每次登录的时候发一次，目前没有做去重)
            self:addActiveBuriedPointAlreadyLogin("dc_mainpage", 0)
        end
    end)
    -- 升级埋点
    EM:on("onPlayerLevelChange", function(name, level)
        self:onPlayerLevelChange(level)
    end)
    --首充礼包埋点
    EM:on("onGetFirstRechangeRewardSuccess", function()
        self:addAdReport({event = "firstpay", channels = {1,2}})
    end)

    -- 累计充值埋点
    EM:on("onPlayerRechargeChange", function(name, recharge)
        self:onPlayerRechargeChange(recharge)
    end)
    -- 高玩用户埋点
    EM:on("onInTheScoreRanking", function()
        self:addAdReport({event = "superGamer", channels = {1,2}})
    end)

    --
    EM:on("OnGameInitStart", function()
        self:addActiveBuriedPointNotLogin("dc_game_init_start", 0)
    end)
    
    EM:on("OnGameInitEnd", function(event, duration)
        self:addActiveBuriedPointNotLogin("dc_game_init_finish", 0, {duration = duration})
    end)
    EM:on("OnFirstMoveEvent", function()
        self:addActiveBuriedPointNotLogin("dc_start_move", 0)
    end)

    EM:on("OnLoginedNoticePopUp", function()

        self:addActiveBuriedPointAlreadyLogin("dc_notice_pop", 0)
    end)
    
    EM:on("OnShowSdkLogin", function()
        self:addActiveBuriedPointNotLogin("dc_sdk_pop", 0)
    end)

    EM:on("OnOverseaFetchServerSuccess", function()
        self:addActiveBuriedPointNotLogin("dc_server_select", 0)
    end)
    
    EM:on("OnGuideBattleAdEvent", handler(self, self.onGuideBattle))
    EM:on("OnNetworkStatusChange", handler(self, self.onNetworkStatusChange))
end

function ADReport:onGameConnectedAdReport()
    if self._isCreateRole then
        self:addAdReport({event = "active"})
    end
end

function ADReport:onLoginDaysAdReport()
    local loginDays = app.session:getLoginDays()
    local eventName = nil
    if loginDays == 2 then
        eventName = "Day2"
    elseif loginDays == 7 then
        eventName = "Day7"
    elseif loginDays == 30 then
        eventName = "Day30"
    elseif loginDays == 60 then
        eventName = "Day60"
    elseif loginDays == 90 then
        eventName = "Day90"
    else
        return
    end
    if eventName ~= nil then
        self:addAdReport({event = eventName, channels = {1,2}})
    end
end

function ADReport:onIsKOLAdReport()
    local isKOL = app.session:getIsKOL()
    if isKOL then
        self:addAdReport({event = "KOL", channels = {1,2}})
    end
end

function ADReport:onPlayerLevelChange(level)
    self:addAdReport({event = "levelup"})
    if level == 2 then
        self:addActiveBuriedPointNotLogin("dc_start_level2", 0)
    elseif level == 13 then
        self:addAdReport({event = "onlevel_13"})
    elseif level == 22 then
        self:addAdReport({event = "onlevel_22"})
    elseif level == 25 then
        self:addAdReport({event = "onlevel_25"})
    end
end

function ADReport:onPlayerRechargeChange(recharge)
    -- 1美元=40钻石
    if not recharge then
        recharge = UD.player.recharge
    end
    if recharge >= 12000 then
        self:addAdReport({event = "largePay", channels = {1,2}})
    elseif recharge >= 6000 then
        self:addAdReport({event = "middlePay", channels = {1,2}})
    elseif recharge >= 2000 then
        self:addAdReport({event = "littlePay", channels = {1,2}})
    end
end

function ADReport:addAdReport(info)
    if not info then return end
    local list = {}
    list.event = info.event
    list.channels = info.channels or {1,2,3,4}
    list.params = {
        account = app.channelAPI:getAccount(),
        server = tostring(app.session:getZoneId()),
        rolename = UD.player.name,
        role_id = UD.player.uid,
        level = UD.player.level,
    }
    if app:isAndroid() then
        list.params[list.event] = AdjustActiveAndroid[list.event]
    elseif app:isIOS() then
        list.params[list.event] = AdjustActiveIOS[list.event]
    end
    app.channelAPI:ADReport(list)
end

function ADReport:onGuideBattle(event, trigger)
    self:addActiveBuriedPointNotLogin(trigger, 0)
end


function ADReport:onNetworkStatusChange(event, trigger)

    self:addActiveBuriedPointAlreadyLogin(trigger, 0)
end
-- 未登陆前 激活率埋点
function ADReport:addActiveBuriedPointNotLogin(trigger, result, otherParams)
    local list = {}
    list.event = "dc_activation_log"
    list.channels = {3,4}
    list.params = {
        trigger = trigger,
        result = result;
    }
    if app:isAndroid() then
        list.params[list.event] = AdjustActiveAndroid[list.event]
    elseif app:isIOS() then
        list.params[list.event] = AdjustActiveIOS[list.event]
    end
    if otherParams then
        for k, v in pairs(otherParams) do
            list.params[k] = v
        end
    end
    if ENABLE_DEBUG_ADREPORD then
        dump(list)
    end
    app.channelAPI:activeBuriedPointReport(list)
end

-- 已登录 激活率埋点
function ADReport:addActiveBuriedPointAlreadyLogin(trigger, result, otherParams)
    local list = {}
    
    local groupId = 0
    if UD.zoneUTC == "UTC+08:00" then
        groupId = 1
    elseif UD.zoneUTC == "UTC-05:00" then
        groupId = 2
    elseif UD.zoneUTC == "UTC+01:00" then
        groupId = 3
    end

    list.event = "dc_activation_log"
    list.channels = {3,4}
    list.params = {
        trigger = trigger,
        result = result,
        server = app.session:getZoneId() or 0,
        role_id = tostring(UD.player.uid) or "",
        account = app.channelAPI:getAccount() or "",
    }

    if groupId ~= 0 then
        list.params.group_id = groupId
    end

    if app:isAndroid() then
        list.params[list.event] = AdjustActiveAndroid[list.event] or ""
    elseif app:isIOS() then
        list.params[list.event] = AdjustActiveIOS[list.event] or ""
    end
    if otherParams then
        for k, v in pairs(otherParams) do
            list.params[k] = v
        end
    end
    if ENABLE_DEBUG_ADREPORD then
        dump(list)
    end
    app.channelAPI:activeBuriedPointReport(list)
end

return ADReport